package com.lanou.managerLogin.book.web.servlet;

import com.lanou.managerLogin.book.domain.Book;
import com.lanou.managerLogin.book.service.BookService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@WebServlet("/insertBookTwoServlet")
public class InsertBookTwoServlet extends HttpServlet {
    private BookService bs = new BookService();
    Book book = new Book();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<String> booklist = new ArrayList<>();
        try {
            DiskFileItemFactory factory = new DiskFileItemFactory();
            String realPath = getServletContext().getRealPath("/");
            factory.setRepository(new File(realPath));

            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setSizeMax(1024 * 1024 * 200);
            upload.setFileSizeMax(1024 * 1024 * 20);
            upload.setHeaderEncoding("UTF-8");

            String path1 = null;
            if (ServletFileUpload.isMultipartContent(req)) {
                List<FileItem> fileItems = upload.parseRequest(req);
                System.out.println(fileItems.size());
                for (FileItem fileItem : fileItems) {
                    if (fileItem.isFormField()) {
                        String key = fileItem.getFieldName();
                        String value = fileItem.getString("UTF-8");
                        booklist.add(value);

                    } else {
                        InputStream input = fileItem.getInputStream();
                        String dir = getServletContext().getRealPath("/book_img");
                        String path = UUID.randomUUID().toString();
                        path1 = "book_img/" + path;
                        File dirFile = new File(dir);
                        if (!dirFile.exists()) {
                            dirFile.mkdirs();
                        }
                        File saveFile = new File(dir, path);
                        if (!saveFile.exists()) {
                            saveFile.createNewFile();
                        }
                        fileItem.write(saveFile);
                        fileItem.delete();

                        input.close();
                    }
                }
                Random random = new Random();
                int i = random.nextInt(10000);
                String bid = String.valueOf(i);
                book.setBid(bid);
                book.setBname(booklist.get(0));
                book.setPrice(Double.parseDouble(booklist.get(1)));
                book.setAuthor(booklist.get(2));
                book.setCid(booklist.get(3));
                System.out.println("==================");
                System.out.println(booklist.get(3));
                book.setImage(path1);
                bs.insertBook(book);
                req.getRequestDispatcher("/adminjsps/admin/book/list.jsp").forward(req, resp);

            } else {
                System.out.println("表单不存在文件！");
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
